namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class OrganRoleBLL
    {
        public int Add(OrganRole model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into OrganRole(");
            builder.Append("RoleName)");
            builder.Append(" values (");
            builder.Append("@RoleName)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RoleName", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = model.RoleName;
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int RoleID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from OrganRole ");
            builder.Append(" where RoleID=@RoleID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.Int, 4) };
            cmdParms[0].Value = RoleID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool Exists(int RoleID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from OrganRole");
            builder.Append(" where RoleID=@RoleID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.Int, 4) };
            cmdParms[0].Value = RoleID;
            return DBHelper.Exists(builder.ToString(), cmdParms);
        }

        public List<OrganRole> GetList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select RoleID,RoleName ");
            builder.Append(" FROM OrganRole ");
            DataSet set = DBHelper.Query(builder.ToString());
            List<OrganRole> list = null;
            OrganRole item = null;
            if (set.Tables[0].Rows.Count > 0)
            {
                list = new List<OrganRole>();
                foreach (DataRow row in set.Tables[0].Rows)
                {
                    item = new OrganRole();
                    if ((row["RoleID"] != null) && (row["RoleID"].ToString() != ""))
                    {
                        item.RoleID = int.Parse(row["RoleID"].ToString());
                    }
                    if ((row["RoleName"] != null) && (row["RoleName"].ToString() != ""))
                    {
                        item.RoleName = row["RoleName"].ToString();
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public OrganRole GetModel(int RoleID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 RoleID,RoleName from OrganRole ");
            builder.Append(" where RoleID=@RoleID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.Int, 4) };
            cmdParms[0].Value = RoleID;
            OrganRole role = new OrganRole();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                if ((set.Tables[0].Rows[0]["RoleID"] != null) && (set.Tables[0].Rows[0]["RoleID"].ToString() != ""))
                {
                    role.RoleID = int.Parse(set.Tables[0].Rows[0]["RoleID"].ToString());
                }
                if ((set.Tables[0].Rows[0]["RoleName"] != null) && (set.Tables[0].Rows[0]["RoleName"].ToString() != ""))
                {
                    role.RoleName = set.Tables[0].Rows[0]["RoleName"].ToString();
                }
                return role;
            }
            return null;
        }

        public bool Update(OrganRole model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update OrganRole set ");
            builder.Append("RoleName=@RoleName");
            builder.Append(" where RoleID=@RoleID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RoleName", SqlDbType.NVarChar, 50), new SqlParameter("@RoleID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.RoleName;
            cmdParms[1].Value = model.RoleID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

